/*!
 * drawer v2.0.0
 *
 * Licensed under MIT
 * Author : blivesta
 *
 */
(function($) {
	"use strict";
	var namespace = "drawer";
	var touches = typeof document.ontouchstart != "undefined";
	var methods = {
		init: function(options) {
			options = $.extend({
				mastaClass: "drawer-main",
				overlayClass: "drawer-overlay",
				toggleClass: "drawer-op",
				upperClass: "drawer-overlay-upper",
				openClass: "drawer-open",
				closeClass: "drawer-close",
				responsiveClass: "drawer-responsive"
			}, options);
			return this.each(function() {
				var _this = this;
				var $this = $(this);
				var data = $this.data(namespace);
				var $upper = $("<div>").addClass(options.upperClass + " " + options.toggleClass);
				if (!data) {
					options = $.extend({}, options);
					$this.data(namespace, {
						options: options
					})
				}
				$this.append($upper);
				var drawerScroll = new IScroll("." + options.mastaClass, {
					scrollbars: true,
					mouseWheel: true,
					click: true,
					fadeScrollbars: true
				});
				$("." + options.toggleClass).off("click." + namespace).on("click." + namespace, function() {
					methods.toggle.call(_this);
					drawerScroll.refresh()
				});
				$(window).resize(function() {
					methods.close.call(_this);
					drawerScroll.refresh()
				})
			})
		},
		toggle: function(options) {
			var _this = this;
			var $this = $(this);
			options = $this.data(namespace).options;
			var open = $this.hasClass(options.openClass);
			open ? methods.close.call(_this) : methods.open.call(_this)
		},
		open: function(options) {
			var $this = $(this);
			options = $this.data(namespace).options;
			var windowWidth = window.innerWidth ? window.innerWidth : $(window).width();
			var upperWidth = windowWidth - $("." + options.mastaClass).outerWidth();
			if (touches) {
				$this.on("touchmove." + namespace, function(event) {
					event.preventDefault()
				})
			}
			$this.removeClass(options.closeClass).addClass(options.openClass).transitionEnd(function() {
				$("." + options.upperClass).css({
					width: upperWidth,
					display: "block"
				});
				$this.css({
					overflow: "hidden"
				})
			})
		},
		close: function(options) {
			var $this = $(this);
			options = $this.data(namespace).options;
			if (touches) {
				$this.off("touchmove." + namespace)
			}
			$("." + options.upperClass).css({
				display: "none"
			});
			$this.removeClass(options.openClass).addClass(options.closeClass).transitionEnd(function() {
				$this.css({
					overflow: "auto"
				});
				$("." + options.upperClass).css({
					display: "none"
				})
			})
		},
		destroy: function() {
			return this.each(function() {
				var $this = $(this);
				$(window).unbind("." + namespace);
				$this.removeData(namespace)
			})
		}
	};
	$.fn.drawer = function(method) {
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1))
		} else if (typeof method === "object" || !method) {
			return methods.init.apply(this, arguments)
		} else {
			$.error("Method " + method + " does not exist on jQuery." + namespace)
		}
	}
})(jQuery);
(function($) {
	"use strict";
	$.fn.transitionEnd = function(callback) {
		var $this = $(this);
		var props = "transitionend webkitTransitionEnd mozTransitionEnd oTransitionEnd MSTransitionEnd";
		if ($this.length > 0) {
			$this.bind(props, function(event) {
				if ($.isFunction(callback)) {
					callback.call($this, event)
				}
			})
		}
		return $this
	};
	$.fn.animationEnd = function(callback) {
		var $this = $(this);
		var props = "animationend webkitAnimationEnd mozAnimationEnd oAnimationEnd MSAnimationEnd";
		if ($this.length > 0) {
			$this.bind(props, function(event) {
				if ($.isFunction(callback)) {
					callback.call($this, event)
				}
			})
		}
		return $this
	}
})(jQuery);